#!/bin/bash
i=0
j=0
data=
function  add_user(){
        echo "请输入添加学员姓名"
        read xm
        echo "请输入学员性别"
        read xb
        echo "请输入学员成绩"
        read cj
	
        data[$i]="$xm:$xb:$cj"
#        echo "data[$i]="${data[$i]}>>abc20.txt
	i=$(($i+1))
        echo -e "是否继续添加\n1：是\n2：否"
        read mun
        if [ $mun -eq 1 ];then
                add_user
        else
		return 1
        fi
	
	
}

function query_user(){
	echo "请输入查询学员名字"
	read	name
	name1=$name
	for j in ${data[@]}
	do
#		echo $j
		name2=`echo ${j} | awk  -F":" '{ print $1 }'`
#		echo $name2
		if [ $name2 == "$name1" ];then
			echo ${j}
			break;
		fi
	done
}

function upate_user(){
	echo "请输入需要更新学员姓名"
	read  name
        name1=$name
        for ((j=0;j<=$(($i-1));j++))
        do
#               echo $j
               name2=`echo ${data[$j]} | awk  -F":" '{ print $1 }'`
#               echo $name2
                if [ $name2 == "$name1" ];then
			echo  "请按以下格式输入更改信息："
			echo "姓名：性别：成绩"
			read name4
			data[$j]="$name4"
                        break;
                fi
		if [ $j -eq $(($i-1)) ];then
			echo "没有这为学员，或请正确输入名字"
		fi
        done
}

ab=`cat abc20.txt`
for z in $ab
do
	data[$i]=$z
#	echo data[$i]=$z
	i=$(($i+1))
done

while true;
do
    echo "1. 添加学员"
    echo "2. 查询学员"
    echo "3. 更新学员"
    echo "4. 退出系统"

    echo -n "请选择:"
    read choice

    if [ $choice -eq 1 ]; then
        add_user
    elif [ $choice -eq 2 ]; then
        query_user
    elif [ $choice -eq 3 ]; then
        upate_user
    elif [ $choice -eq 4 ]; then
	echo "">abc20.txt
	echo "是否保存？yes/no"
	read yn
	if [ $yn == "yes" ];then
		for j2 in ${data[@]};
		do
			echo $j2>>abc20.txt
		done
		exit 0
	else
		exit 0
	fi
    else
        echo "错误的选择!!!"
    fi
done


